package com.captain.leetcode;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author captain
 * @Des
 * @Date 2021/7/20 21:11
 */
public class TreeNode {


    public Integer val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode() {
    }


    public TreeNode(Integer val) {
        this.val = val;
    }

    TreeNode(Integer val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    public Integer getVal() {
        return val;
    }

    public void setVal(Integer val) {
        this.val = val;
    }

    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }

    public static TreeNode getInstance() {
        return new TreeNode();
    }

    static Deque<TreeNode> stack = new LinkedList<>();
    static int index = 1;
    public static List<TreeNode> nodeList = new LinkedList<>();

    public static TreeNode spanningTree(Integer[] arr) {
        for (Integer value : arr) {
            nodeList.add(new TreeNode(value));
        }
        TreeNode node = nodeList.get(0);
        stack.add(node);
        while (!stack.isEmpty()) {
            int size = stack.size();
            for (int i = 0; i < size; i++) {
                TreeNode pop = stack.pop();
                if (pop.val == null) {
                    continue;
                }
                if (index > nodeList.size() - 1) {
                    continue;
                }
                pop.left = nodeList.get(index).val == null ? null : nodeList.get(index);
                stack.addLast(nodeList.get(index));
                index++;
                if (index > nodeList.size() - 1) {
                    continue;
                }
                pop.right = nodeList.get(index).val == null ? null : nodeList.get(index);
                stack.addLast(nodeList.get(index));
                index++;
            }
        }
        return node;
    }
}
